package web.beans;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import ejb.dto.PacchettoDTO;
import ejb.managerbeans.PackManagerBeanLocal;

@ManagedBean(name = "listPacchettiBean")
public class ListPacchettiBean {
	@EJB
	PackManagerBeanLocal pmb;

	@ManagedProperty(value = "#{giftlistBean}")
	private GiftListBean gf;

	private List<PacchettoDTO> listPacchetti;
	private List<PacchettoDTO> listPacchettiUser;
	private PacchettoDTO pToAdd;
	private String errore;

	@PostConstruct
	public void init() {
		try {
			listPacchetti = pmb.getAllPacks();
			listPacchettiUser = pmb.getAllPacksOfUserLogged();
		} catch (Exception e) {
		}
	}

	public void buy(PacchettoDTO pacchetto) {
		pmb.boughtPack(pacchetto);
	}

	public List<PacchettoDTO> getListPacchetti() {
		return listPacchetti;
	}

	public void setListPacchetti(List<PacchettoDTO> listPacchetti) {
		this.listPacchetti = listPacchetti;
	}

	public String openPage() {
		return "modificapacchetto?faces-redirect=true";
	}

	public String eliminaPacchetto(int id) {
		pmb.eliminaPacchetto(id);
		return "listpack?faces-redirect=true";
	}

	public String eliminaPacchettoUtente(int id) {
		pmb.eliminaPacchetto(id);
		return "listpack?faces-redirect=true";
	}

	public List<PacchettoDTO> getListPacchettiUser() {
		return listPacchettiUser;
	}

	public void setListPacchettiUser(List<PacchettoDTO> listPacchettiUser) {
		this.listPacchettiUser = listPacchettiUser;
	}

	public String getErrore() {
		return errore;
	}

	public void setErrore(String errore) {
		this.errore = errore;
	}

	public PacchettoDTO getpToAdd() {
		return pToAdd;
	}

	public void setpToAdd(PacchettoDTO pToAdd) {
		this.pToAdd = pToAdd;
	}

	public GiftListBean getGf() {
		return gf;
	}

	public void setGf(GiftListBean gf) {
		this.gf = gf;
	}

	public String addToGF() {

		// System.out.println("trying to add to giftlist");

		gf.setRenderAddingSection(true);

		gf.setP(pToAdd);
		gf.setAddedProd(1);
		gf.populateListForAdding();

		return "aggiungiAGiftList?faces-redirect=true";

	}

}
